package my.algorithms.mcorrea.datastructures;

/**
 *
 * @author mcorrea
 */
public class Queue {

	private int data[];
	private int head;
	private int tail;
	private int MAX_CAPACITY = 10;
	
	public Queue(){
		this.data = new int[MAX_CAPACITY];
		this.head = -1;
		this.tail = 0;
	}
	
	public void enqueue(int x){
		if(tail+1 != head || (tail+1 == data.length && head != 0 ) ){//avoid overflow
			data[tail] = x;//put the object in the tail
			if(tail+1 == data.length){
				tail = 0;//circular array
			}else{
				tail++;
			}
			if(head ==-1){
				head =0;
			}
		}	
	}
	
	
	public int dequeue(){
		if( head != -1 ){
			if(head == tail){ //avoid underflow
				return -1;
			}
			int x = data[head];
			if(head == data.length){
				head = 0;
			}else{
				head++;
			}
			return x; 
		}
		return -1; //empty
	}
	
	public static void main(String args[]){
		Queue q = new Queue();
		q.enqueue(1);
		q.enqueue(2);
		q.enqueue(3);
		
		System.out.println(q.dequeue());
		System.out.println(q.dequeue());
		System.out.println(q.dequeue());
		System.out.println(q.dequeue());
		q.enqueue(4);
		System.out.println(q.dequeue());
		System.out.println(q.dequeue());
		
	}
	
}
